package com.hospital.warehouse.recovery.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.util.DTO;
import com.hospital.util.JMyUtil;
import com.hospital.warehouse.drugstock.dao.DrugstockDao;
import com.hospital.warehouse.drugstock.entity.Drugstock;
import com.hospital.warehouse.drugstock.service.DrugstockService;
import com.hospital.warehouse.exception.WarehouseException;
import com.hospital.warehouse.myenum.Source;
import com.hospital.warehouse.myenum.State;
import com.hospital.warehouse.recovery.dao.RecoveryDao;
import com.hospital.warehouse.recovery.entity.Recovery;
import com.hospital.warehouse.recovery.service.RecoveryService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr. YR
 * @since 2020-04-28
 */
@Service
public class RecoveryServiceImpl extends ServiceImpl<RecoveryDao, Recovery> implements RecoveryService {

	@Autowired
	RecoveryDao recoveryDao;
	@Autowired
	DrugstockService drugstockService;
	@Autowired
	DrugstockDao drugStockDao;

	@Override
	public DTO paging(Recovery po, Integer page, Integer limit) throws ParseException {
//		System.out.println(po);

		if (JMyUtil.notEmpty(po.getRe_recoverytime_interval())) {
			String[] dates = po.getRe_recoverytime_interval().split(" ~ ");
			po.setRe_recoverytime_min(JMyUtil.formatStringToDate_yMdHms(dates[0]));
			po.setRe_recoverytime_max(JMyUtil.formatStringToDate_yMdHms(dates[1]));
		}
		if (JMyUtil.notEmpty(po.getRe_confirmtime_interval())) {
			String[] dates = po.getRe_confirmtime_interval().split(" ~ ");
			po.setRe_confirmtime_min(JMyUtil.formatStringToDate_yMdHms(dates[0]));
			po.setRe_confirmtime_max(JMyUtil.formatStringToDate_yMdHms(dates[1]));
		}

		Page<Recovery> pageinfo = new Page<>(page, limit);

		IPage<Recovery> iPage = recoveryDao.paging(po, pageinfo, null);
		List<Recovery> list = iPage.getRecords();

		DTO dto = new DTO();
		dto.setCount(iPage.getTotal());
		dto.setData(list);

		if (list.size() <= 0) {
			dto.setMsg("没有记录");
		}

		return dto;
	}

	@Override
	public Recovery queryById(Integer id) {
		Recovery po = recoveryDao.queryById(id);
		return po;
	}

	@Transactional
	@Override
	public void saveOne(Recovery po, HttpSession session) {
//		System.out.println(po);
		// 设置状态
		po.setRe_state(State.等待核实.name());
		// 新增返回自增id
		po.insert();
	}

	@Transactional
	@Override
	public void updateOne(Recovery po) {
//		System.out.println(po);
		String re_source = po.getRe_source();
		if (Source.来自药房.name().equals(re_source)) {
			// 来自药房时，必须有药房id
			if (po.getRe_frompharmacyid() == null) {
				throw new WarehouseException("请选择药房");
			}
		} else {
			// 其它情况都设置药房列为空
			po.setRe_frompharmacyid(null);
		}
		// 修改数据方法
		po.updateById();
	}

	@Override
	public void updateState(Integer id, String state, HttpSession session) {
		Recovery po = new Recovery();
		if (JMyUtil.isEmpty(id)) {
			throw new WarehouseException("请选择回收单");
		}
		// this代表当前service实现类
		Recovery oldRe = this.queryById(id);
		if ("confirm".equals(state)) {
			// 如果从库房回收对象，则同时减少库存
			if (Source.来自库房.name().equals(oldRe.getRe_source())) {
				// 查询当前库存对象
				Drugstock drugstock = drugStockDao.queryById(oldRe.getRe_is_id());
				// 查询库存数量
				Integer ds_num = drugstock.getDs_num();
				// 查询即将回收数量
				Integer re_num = oldRe.getRe_num();
				// 如果库存数量小于要回收数量
				if (ds_num < re_num) {
					throw new WarehouseException("库存不足，请核实！");
				}
				// 同时减少库存
				drugstock.setDs_num(ds_num - re_num);
				drugstock.updateById();
			}
			// 设置状态
			po.setRe_state(State.已回收.name());
		} else if ("cancel".equals(state)) {
			po.setRe_state(State.已取消.name());
		} else {
			throw new WarehouseException("请选择状态");
		}
		po.setRe_id(id);
		po.setRe_frompharmacyid(oldRe.getRe_frompharmacyid());
//		Sys_user user = (Sys_user) session.getAttribute("user");
//		user.getUser_id();
		po.setRe_confirmid(new Random().nextInt(2) + 1);
		po.setRe_confirmtime(new Date());

		po.updateById();
	}
}
